import { getZkpList } from "@api"
import type { ZkbbAddEmits, ZkbbAddProps } from "./prop"
import type { SetupContext } from "vue"
import { EventSure } from "@ui/utils/var"
export function use(props: ZkbbAddProps, emit: SetupContext<ZkbbAddEmits>['emit']) {
  const table = ref(),
    tableData = ref<any[]>([]),
    addFormData = ref({ jsm: '', xmlb: '', xjbz: '', yq: props.yqid })
  watch(() => props.yqid, query, { immediate: true })
  function query() {
    const { jsm, yq } = addFormData.value
    getZkpList({ yqid: yq, jsm }).then(({ data: { list } }) => {
      tableData.value = list
    })
  }
  function addRest() {
    addFormData.value = { yqid: props.yqid } as any
  }
  function save() {
    if (table.value) {
      emit(EventSure, table.value.getSelectionRows())
    }
  }
  function clear() {
    if (table.value)
      table.value.clearSelection()
  }
  return { query, save, addFormData, addRest, tableData, table, clear }
}